<script setup>
import { ref } from 'vue'
import { throttle, calculateStartPosition, calculateNewPosition } from '@/utils'

const props = defineProps({
  showList: {
    type: Array,
    required: true
  }
})

const position = ref({ x: 510, y: 30 })
const isDragging = ref(false)
const startPosition = ref({ x: 0, y: 0 })


const startDrag = (e) => {

  isDragging.value = true
  startPosition.value = calculateStartPosition(e, position.value)

  window.addEventListener('mousemove', onDrag)
  window.addEventListener('mouseup', stopDrag)
}

const updatePosition = (e) => {
  if (isDragging.value) {
    position.value = calculateNewPosition(e, startPosition.value)
  }
}

const onDrag = throttle(updatePosition, 16)

const stopDrag = () => {
  isDragging.value = false
  window.removeEventListener('mousemove', onDrag)
  window.removeEventListener('mouseup', stopDrag)
}


</script>
<template>
  <div class="state-box need-zoom" @mousedown="startDrag" :style="{ transform: `translate(${position.x}px, ${position.y}px)` }">
    <ul>
      <li class="show-item" v-for="item in props.showList">
        {{ item.translation }}
      </li> 
    </ul>
  </div>
</template>

<style>
.state-box {
  z-index: 510;
  position: absolute;
  width: 246px;
  height: 180px;
  border-radius: 14px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  background-image: url("../../assets/images/card-bg.png");
  background-size: contain;
  font-size: 18px;
  line-height: 26px;
  user-select: none;
}

</style>
